package com.zs.blog.controller;

import com.zs.blog.constant.enums.ResponseResultEnum;
import com.zs.blog.model.ResponseResult;
import com.zs.blog.model.dto.UserLoginDTO;
import com.zs.blog.model.dto.UserSearchDTO;
import com.zs.blog.model.dto.UserUpdateTypeDTO;
import com.zs.blog.model.entity.User;
import com.zs.blog.model.vo.PageInfoVO;
import com.zs.blog.model.vo.UserDetailVO;
import com.zs.blog.model.vo.UserListVO;
import com.zs.blog.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.ui.freemarker.SpringTemplateLoader;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: UserController
 * @Author zs
 * @Package com.zs.blog.controller
 * @Date 2025/4/14 23:03
 * @description: 用户接口层
 */
@RestController
@Tag(name = "用户相关接口")
public class UserController {

    public IUserService userService;

    public StringRedisTemplate stringRedisTemplate;

    private UserController(IUserService userService, StringRedisTemplate stringRedisTemplate) {
        this.userService = userService;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @PostMapping("/portal/user/login")
    @Operation(summary = "用户登录")
    public ResponseResult<UserLoginDTO> login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {
        UserLoginDTO data = userService.login(username, password);
        return ResponseResult.success(data);
    }

    @PostMapping("/portal/user/register")
    @Operation(summary = "用户注册")
    public ResponseResult<?> register(@RequestBody @Validated User user) {
        userService.register(user);
        return ResponseResult.success();
    }

    @GetMapping("/admin/user/list")
    @Operation(summary = "获取分页的用户信息列表")
    public ResponseResult<PageInfoVO<UserListVO>> listUserWithPagination(Integer pageNum, Integer pageSize) {
        PageInfoVO<UserListVO> listUser = userService.listUserWithPagination(pageNum, pageSize);
        return ResponseResult.success(listUser);
    }

    @GetMapping("/admin/user/detail/{userId}")
    @Operation(summary = "根据用户ID获取用户详情")
    public ResponseResult<UserDetailVO> getUserDetailByUserId(@PathVariable Long userId) {
        UserDetailVO userDetail = userService.getUserDetailByUserId(userId);
        return ResponseResult.success(userDetail);
    }

    @PutMapping("/admin/user/update/type")
    @Operation(summary = "更新用户状态")
    public ResponseResult<?> updateUserType(@RequestBody UserUpdateTypeDTO userUpdateTypeDTO) {
        userService.updateUserType(userUpdateTypeDTO);
        return ResponseResult.success();
    }

    @PostMapping("/admin/user/search")
    @Operation(summary = "通过搜索条件查询分页的用户列表")
    public ResponseResult<PageInfoVO<UserListVO>> listUserBySearchCondition(@RequestBody UserSearchDTO userSearchDTO) {
        PageInfoVO<UserListVO> listUser = userService.listUserBySearchCondition(userSearchDTO);
        return ResponseResult.success(listUser);
    }

    @DeleteMapping("/admin/user/delete")
    @Operation(summary = "删除用户信息")
    public ResponseResult<?> deleteUser(List<Long> userIdList){
        userService.deleteUserByIds(userIdList);
        return ResponseResult.success();
    }

}
